package util;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.FileInputStream;
import java.util.List;

public class PathUtil {

    /**
     * 获取右键选中位置包名
     *
     * @param event
     * @return
     */
    public static String readPackageName(AnActionEvent event) {
        VirtualFile virtualFile = event.getData(CommonDataKeys.VIRTUAL_FILE);
        String path = virtualFile.getPath();
        String[] array = path.split("/src/main/java/");
        if (array.length > 1) {
            return array[1].replace("/", ".");
        }
        return null;
    }

    /**
     * 获取生成文件的保存路径
     *
     * @param event
     * @return
     */
    public static String getFileSavePath(AnActionEvent event) {
        VirtualFile virtualFile = event.getData(CommonDataKeys.VIRTUAL_FILE);
        String path = virtualFile.getPath();
        return path;
    }
    /**
     * 获取选中项目的appid
     *
     * @param project
     * @return
     */
    public static String readApplicationId(Project project) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            String manifestPath = project.getBasePath() + "/app/src/main/AndroidManifest.xml";
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(new FileInputStream(manifestPath)));
            NodeList dogList = doc.getElementsByTagName("manifest");
            String mPackage = null;
            for (int i = 0; i < dogList.getLength(); i++) {
                Node dog = dogList.item(i);
                Element elem = (Element) dog;
                mPackage = elem.getAttribute("package");
            }
            if (mPackage == null || mPackage.equals("")) {// manifest.xml中读取不到则需要从build.gradle中读取
                String buildFilePath = project.getBasePath() + "/app/build.gradle";
                List<String> lines = FileUtils.readLines(buildFilePath);
                for (String line : lines) {
                    if (line.contains("applicationId")) {
                        int startIndex = line.indexOf("\"");
                        int endIndex = line.lastIndexOf("\"");
                        mPackage = line.substring(startIndex+1, endIndex);
                    }
                }
            }
            return mPackage;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
    /**
     * 获取选中module的appid
     *
     * @param rightClickPath 右键路径
     * @return
     */
    public static String readModuleApplicationId(String rightClickPath) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            int begin = rightClickPath.indexOf("/src/main/java");
            String manifestPath = rightClickPath.substring(0, begin) + "/src/main/AndroidManifest.xml";
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = db.parse(new InputSource(new FileInputStream(manifestPath)));
            NodeList dogList = doc.getElementsByTagName("manifest");
            String mPackage = null;
            for (int i = 0; i < dogList.getLength(); i++) {
                Node dog = dogList.item(i);
                Element elem = (Element) dog;
                mPackage = elem.getAttribute("package");
            }
            if (mPackage == null || mPackage.equals("")) {// manifest.xml中读取不到则需要从build.gradle中读取
                String buildFilePath = rightClickPath.substring(0, begin) + "/build.gradle";
                List<String> lines = FileUtils.readLines(buildFilePath);
                for (String line : lines) {
                    if (line.contains("applicationId")) {
                        int startIndex = line.indexOf("\"");
                        int endIndex = line.lastIndexOf("\"");
                        mPackage = line.substring(startIndex + 1, endIndex);
                    }
                }
            }
            return mPackage;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 刷新项目目录
     */
    public static void refreshProject(AnActionEvent e) {
        e.getProject().getBaseDir().refresh(false, true);
    }


}
